﻿<!doctype html>
<html>
<head>
	<meta charset="utf-8" />
	<title>优化上下架时间</title>
    <link href="css/mycss.css" rel="stylesheet" type="text/css">
    <link href="http://docs.kissyui.com/kissy-gallery/gallery/grid/1.0/assets/grid.css" rel="stylesheet" type="text/css">
    <link href="http://a.tbcdn.cn/s/kissy/1.3.0/css/dpl/??base-min.css,badges-min.css,icons-min.css,labels-min.css,tables-min.css" rel="stylesheet" type="text/css">
    <link href="http://a.tbcdn.cn/s/kissy/1.3.0/??button/assets/dpl-min.css,calendar/assets/dpl-min.css,menu/assets/dpl-min.css,combobox/assets/dpl-min.css,menubutton/assets/dpl-min.css,overlay/assets/dpl-min.css,split-button/assets/dpl-min.css,tabs/assets/dpl-min.css,toolbar/assets/dpl-min.css,tree/assets/dpl-min.css" rel="stylesheet" type="text/css">
	<script src="http://a.tbcdn.cn/s/kissy/1.3.0/kissy.js" charset="utf-8"></script>
	<script src="dwr/interface/OnSaleService.js"></script>
	<script src="dwr/engine.js"></script>
	<script type="text/javascript">
		KISSY.getScript("http://l.tbcdn.cn/apps/top/x/sdk.js?appkey="+KISSY.Cookie.get("appkey"));
	</script>
</head>
<body>
	<!--主体内容-->
	<div id="content">
	    <!-- 功能描述 -->
	    <div style="padding: 20px 0px;">
	        <div class="ks-popup-inner">
	            <h3 class="ks-popup-title">自动优化上下架时间</h3>
	            <div class="ks-popup-content">
	                <p>And here's some amazing content. It's very engaging. right?</p>
	            </div>
	        </div>
	    </div>
	    <!-- 自动上架 -->
	    <div>
	        <div style="padding: 0px 15px;">
	            <label id="itemInfo">您店铺共有50件商品</label>
	        </div>
        	<div id="itemIcon" class="itemIcon">
        		<i class="icon-plus" data="1"></i><i class="icon-minus" data="1"></i>
        		<i class="icon-plus" data="2"></i><i class="icon-minus" data="2"></i>
        		<i class="icon-plus" data="3"></i><i class="icon-minus" data="3"></i>
        		<i class="icon-plus" data="4"></i><i class="icon-minus" data="4"></i>
        		<i class="icon-plus" data="5"></i><i class="icon-minus" data="5"></i>
        		<i class="icon-plus" data="6"></i><i class="icon-minus" data="6"></i>
        		<i class="icon-plus" data="7"></i><i class="icon-minus" data="7"></i>
        		<i class="icon-plus" data="8"></i><i class="icon-minus" data="8"></i>
        		<i class="icon-plus" data="9"></i><i class="icon-minus" data="9"></i>
        		<i class="icon-plus" data="10"></i><i class="icon-minus" data="10"></i>
        		<i class="icon-plus" data="11"></i><i class="icon-minus" data="11"></i>
        		<i class="icon-plus" data="12"></i><i class="icon-minus" data="12"></i>
        		<i class="icon-plus" data="13"></i><i class="icon-minus" data="13"></i>
        		<i class="icon-plus" data="14"></i><i class="icon-minus" data="14"></i>
        		<i class="icon-plus" data="15"></i><i class="icon-minus" data="15"></i>
        		<i class="icon-plus" data="16"></i><i class="icon-minus" data="16"></i>
        		<i class="icon-plus" data="17"></i><i class="icon-minus" data="17"></i>
        		<i class="icon-plus" data="18"></i><i class="icon-minus" data="18"></i>
        		<i class="icon-plus" data="19"></i><i class="icon-minus" data="19"></i>
        		<i class="icon-plus" data="20"></i><i class="icon-minus" data="20"></i>
        		<i class="icon-plus" data="21"></i><i class="icon-minus" data="21"></i>
        		<i class="icon-plus" data="22"></i><i class="icon-minus" data="22"></i>
        		<i class="icon-plus" data="23"></i><i class="icon-minus" data="23"></i>
        		<i class="icon-plus" data="24"></i><i class="icon-minus" data="24"></i>
        	</div>
        	<div id="icomItem" class="icomItem">
        		<i class="icon-plus" data="0"></i><i class="icon-minus" data="0"></i>
        		<i class="icon-plus" data="1"></i><i class="icon-minus" data="1"></i>
        		<i class="icon-plus" data="2"></i><i class="icon-minus" data="2"></i>
        		<i class="icon-plus" data="3"></i><i class="icon-minus" data="3"></i>
        		<i class="icon-plus" data="4"></i><i class="icon-minus" data="4"></i>
        		<i class="icon-plus" data="5"></i><i class="icon-minus" data="5"></i>
        		<i class="icon-plus" data="6"></i><i class="icon-minus" data="6"></i>
        	</div>
        	<div id="grid"></div>
        	<div id="gridError" class="gridError"> 
				<label> 
					<span class="annotation">
						<em class="onlabel"></em> 
					</span> 
				</label> 
			 </div>
	        <div style="float:right;padding:10px 0px;">
	            <div id="save" class="ks-button ks-button-primary">保存计划</div>
	            <div id="log" class="ks-button ks-button-primary">查看日志</div>
	        </div>
	        <script type="text/javascript">
	        KISSY.config({
			    packages: [{
			        name: "gallery",
			        tag: "201112081542",
			        path: "http://docs.kissyui.com/kissy-gallery",
			        charset: 'utf-8'
			    }]
			});
			KISSY.use('gallery/grid/1.0/',function(S, Grid) {TOP.api("tql", "get", {
			        ql: "select num_iid from taobao.items.onsale.get",
			        session: TOP.cookie.get("session")
			    },function(response) {
			        var totalResults = response.items_onsale_get_response.total_results;
			        S.one("#itemInfo").html("您店铺共有" + totalResults + "件商品");
			        store = new Grid.Store({
			            autoLoad: false
			        }),
			        items = new Array({
			            title: "#",
			            width: 45,
			            dataIndex: '0',
			            editor: {
			                type: 'text',
			                editableFun: function(value, obj) {
			                    return false;
			                }
			            }
			        });　 //创建一个数组
			        for (var i = 1; i < 25; i++) {
			            items[i] = {
			                title: i + '时',
			                width: 37,
			                dataIndex: i.toString(),
			                editor: {
			                    type: 'number',
			                    validator: function(value, obj) {
			                        if (value > totalResults) {
			                            return '不能超过您店铺的宝贝总数量(' + totalResults + '件)';
			                        }
			                    }
			                },
			                renderer: function(value) {
			                    if (value) return value;
			                }
			            };
			        }
			        config = {
			            renderTo: 'grid',
			            width: 985,
			            columns: items,
			            //showError:true,
			            store: store
			        };
			        var grid = new Grid.EditGrid(config);
			        var size = parseInt(totalResults / 63);
			        var number = totalResults % 63;
			        //设置数据
			        var week = new Array("星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日");
			        var items = new Array();
			        OnSaleService.getPlan(function(data) {
			            var total = totalResults;
			            for (var i = 0; i < week.length; i++) {
			                items[i] = new Array(week[i]);
			                for (var j = 1; j < 25; j++) {
			                    //如果已经有记录
			                    if (data!=null && data.plan) {
			                        if (data.plan[i][j - 1]) {
			                            total -= parseInt(data.plan[i][j - 1]);
			                        }
			                        items[i][j] = parseInt(data.plan[i][j - 1]);
			                    } else {
			                        items[i][j] = 0;
			                        if (j > 8 && j < 14) {
			                            items[i][j] = size;
			                            if ((i * 9 + (j - 8)) <= number) {
			                                items[i][j] = size + 1;
			                            }
			                        }
			                        if (j > 16 && j < 21) {
			                            items[i][j] = size;
			                            if ((i * 9 + (j - 11)) <= number) {
			                                items[i][j] = size + 1;
			                            }
			                        }
			                    }
			                }
			            }
			            if (data && total > 0) {
			                change(items, total);
			            }
			            store.setResult(items);
			        });
			
			        function change(list, total) {
			            for (var i = 0; i < list.length; i++) {
			                for (var j = 1; j < list[i].length; j++) {
			                    if (list[i][j] && total > 0) {
			                        total--;
			                        list[i][j]++;
			                    }
			                }
			            }
			            if (total > 0) {
			                change(list, total);
			            }
			        };
			
			        //点击时间
			        S.all('#itemIcon i').on('click',function(event) {
			            var index = parseInt(event.currentTarget.getAttribute('data'));
			            for (var i = 0; i < items.length; i++) {
			                for (var j = 1; j < items[i].length; j++) {
			                    if (index == j && event.currentTarget.className == "icon-plus" && items[i][j] < totalResults) {
			                        items[i][j] += 1;
			                    } else if (index == j && items[i][j] > 0) {
			                        items[i][j] -= 1;
			                    }
			                }
			            }
			            store.setResult(items);
			        });
			        //点击星期
			        S.all('#icomItem i').on('click',function(event) {
			            var index = parseInt(event.currentTarget.getAttribute('data'));
			            for (var i = 0; i < items.length; i++) {
			                for (var j = 1; j < items[i].length; j++) {
			                    if (index == i && event.currentTarget.className == "icon-plus" && items[i][j] < totalResults) {
			                        items[i][j] += 1;
			                    } else if (index == i && items[i][j] > 0) {
			                        items[i][j] -= 1;
			                    }
			                }
			            }
			            store.setResult(items);
			        });
					//显示错误
					function showError(msg){
						var	textEl = S.one('.onlabel','#gridError');
						textEl.text(msg);
						S.one('#gridError').show();
		 
					}
					//清理错误
					function clearError(){
						var textEl = S.one('.onlabel','#gridError');
						textEl.text('');
						S.one('#gridError').hide();
					}
			        //点击保存
			        S.one("#save").on('click',function(event) {
			            var list = new Array();
			            for (var i = 0; i < items.length; i++) {
			                list[i] = new Array();
			                for (var j = 1; j < items[i].length; j++) {
			                    list[i][j - 1] = items[i][j];
			                }
			            }
			            var records = store.getResult(),validate = true;
						if(grid.hasError()){
							event.halt();
							showError('亲，您输入的数字格式不正确！');
							return;
						}
						clearError();
			            OnSaleService.savePlan(list,totalResults);
			        });
			        //查看日志
			        S.one("#log").on('click',function(event){
			        	parent.document.getElementById("content").height = 1020;
						parent.document.getElementById("content").src = "onsaleReport.html";
			        });
			    });
			});
	        </script>
	    </div>
	</div>
</body>
</html>
